package database.databaseAccessObjects;



import java.util.ArrayList;
import java.util.List;

import database.DatabaseHelpers.ActionConstants;
import database.DatabaseHelpers.CheckInConstants;
import database.DatabaseHelpers.LocationConstants;
import database.DatabaseHelpers.VIPmeSQLiteHelper;
import database.Models.Action;
import database.Models.CheckIn;
import database.Models.Location;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DAOAction {
//has many checkins
	// Database fields
	private SQLiteDatabase database;
	private VIPmeSQLiteHelper dbHelper;
	private String[] allColumns = { ActionConstants.COLUMN_ID,
			ActionConstants.COLUMN_NAME,ActionConstants.COLUMN_LOCATION_ID, ActionConstants.COLUMN_TEXT };

	public DAOAction(Context context) {
		dbHelper = new VIPmeSQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Action createAction(String name,String text,int location_id) {
		ContentValues values = new ContentValues();
		values.put(ActionConstants.COLUMN_NAME, name);
		values.put(ActionConstants.COLUMN_TEXT, text);
		values.put(ActionConstants.COLUMN_LOCATION_ID, location_id);
		long insertId = database.insert(ActionConstants.TABLE_ACTION, null,
				values);
		Cursor cursor = database.query(ActionConstants.TABLE_ACTION,
				allColumns, ActionConstants.COLUMN_ID + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		Action newLoc = cursorToLocation(cursor);
		cursor.close();
		return newLoc;
	}

	public void deleteLocation(Action action) {
		long id = action.getId();
		System.out.println("Comment deleted with id: " + id);
		database.delete(ActionConstants.TABLE_ACTION, ActionConstants.COLUMN_ID
				+ " = " + id, null);
	}
	public Action getActionById(int actionID) {
		Cursor cursor = database.query(ActionConstants.TABLE_ACTION,allColumns, ActionConstants.COLUMN_ID
				+ " = " + actionID, null, null, null, null);
		cursor.moveToFirst();
		return cursorToLocation(cursor);
	}
	public List<Action> getAllAction() {
		List<Action> Actions = new ArrayList<Action>();

		Cursor cursor = database.query(ActionConstants.TABLE_ACTION,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Action comment = cursorToLocation(cursor);
			Actions.add(comment);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return Actions;
	}
	public List<Action> getAllActionsForLocation(int locationID) {
		List<Action> cis = new ArrayList<Action>();

		Cursor cursor = database.query(ActionConstants.TABLE_ACTION,
				allColumns, ActionConstants.COLUMN_LOCATION_ID
				+ " = " + locationID, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Action ci = cursorToLocation(cursor);
			cis.add(ci);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return cis;
	}

	private Action cursorToLocation(Cursor cursor) {
		Action action = new Action();
		action.setId(cursor.getInt(0));
		action.setName(cursor.getString(1));
		action.setDisco_id(Integer.parseInt(cursor.getString(2)));
		action.setText(cursor.getString(3));
		return action;
	}
}

